package com.yf.catcafe.controller;

import com.yf.catcafe.pojo.dto.AddCafeDto;
import com.yf.catcafe.pojo.dto.CafeDto;
import com.yf.catcafe.pojo.dto.OrderVo;
import com.yf.catcafe.pojo.enity.Cafe;
import com.yf.catcafe.pojo.enity.Order;
import com.yf.catcafe.pojo.vo.CoffeQueryVo;
import com.yf.catcafe.result.Result;
import com.yf.catcafe.service.CoffesService;
import com.yf.catcafe.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/catcafe")
@CrossOrigin("*")
public class CoffeController {

    @Autowired
    private CoffesService coffesService;

    @Autowired
    private OrdersService ordersService;

    private static final String UPLOAD_DIR = "D:/CafeUploads/";

    @GetMapping("/coffees")
    public Result<List<CoffeQueryVo>> query(){
        List<Cafe> cafes=coffesService.query();
        List<CoffeQueryVo> coffeQueryVos=cafes.stream().map(cafe -> {
            CoffeQueryVo vo=new CoffeQueryVo();
            vo.setId(cafe.getId());
            vo.setDescription(cafe.getDescription());
            vo.setPrice(cafe.getPrice());
            vo.setImage(cafe.getImage());
            vo.setCategory(cafe.getCategory());
            vo.setName(cafe.getName());
            return vo;
        }).collect(Collectors.toList());
        return  Result.success(coffeQueryVos);
    }

    @GetMapping("/coffees/{coffeeId}")
    public Result<CoffeQueryVo> getDetails(@PathVariable(value = "coffeeId") Long id){
         Cafe cafe=coffesService.getDetails(id);
         CoffeQueryVo vo=new CoffeQueryVo();
         vo.setName(cafe.getName());
         vo.setDescription(cafe.getDescription());
         vo.setId(cafe.getId());
         vo.setCategory(cafe.getCategory());
         vo.setImage(cafe.getImage());
         vo.setPrice(cafe.getPrice());
         return Result.success(vo);
    }

    @PostMapping("/orders")
    public Result<OrderVo> order(@RequestBody CafeDto cafeDto){
        OrderVo orderVo=ordersService.insert(cafeDto);
        return Result.success(orderVo);
    }

    @GetMapping("/coffees/category")
    public Result<List<CoffeQueryVo>> queryByCatefory(@RequestParam(value = "category") String category){
        List<Cafe> cafes=coffesService.queryByCategory(category);
        List<CoffeQueryVo> coffeeQueryVos=cafes.stream().map(cafe -> {
            CoffeQueryVo vo=new CoffeQueryVo();
            vo.setPrice(cafe.getPrice());
            vo.setDescription(cafe.getDescription());
            vo.setName(cafe.getName());
            vo.setImage(cafe.getImage());
            vo.setCategory(cafe.getCategory());
            vo.setId(cafe.getId());
            return vo;
        }).collect(Collectors.toList());
        return Result.success(coffeeQueryVos);
    }

    @GetMapping("/coffees/search")
    public Result<List<CoffeQueryVo>> queryByName(@RequestParam(value = "name") String name){
        List<Cafe> cafes=coffesService.queryByName(name);
        List<CoffeQueryVo> coffeQueryVos=cafes.stream().map(cafe -> {
            CoffeQueryVo vo=new CoffeQueryVo();
            vo.setPrice(cafe.getPrice());
            vo.setDescription(cafe.getDescription());
            vo.setName(cafe.getName());
            vo.setImage(cafe.getImage());
            vo.setCategory(cafe.getCategory());
            vo.setId(cafe.getId());
            return vo;
        }).collect(Collectors.toList());
        return Result.success(coffeQueryVos);
    }

    @PostMapping("/coffees/add")
    public Result add(@RequestParam("name") String name,
                      @RequestParam("category") String category,
                      @RequestParam("price") BigDecimal price,
                      @RequestParam("description") String descriptioon,
                      @RequestParam("image") MultipartFile image) throws IOException {

        // 保存图片到服务器
        String fileName = UUID.randomUUID() + "_" + image.getOriginalFilename();
        Path filePath = Paths.get(UPLOAD_DIR + fileName);
        image.transferTo(filePath);

        AddCafeDto addCafeDto=new AddCafeDto();
        addCafeDto.setCategory(category);
        addCafeDto.setDescription(descriptioon);
        addCafeDto.setName(name);
        addCafeDto.setImage("/uploads/"+fileName);
        addCafeDto.setPrice(price);

        coffesService.add(addCafeDto);
        return Result.success();
    }

    @GetMapping("/seckillcoffees")
    public Result<List<CoffeQueryVo>> seckillcoffees(){
        List<Cafe> cafes=coffesService.seckillCafe();
        List<CoffeQueryVo> coffeQueryVos=cafes.stream().map(cafe -> {
            CoffeQueryVo vo=new CoffeQueryVo();
            vo.setId(cafe.getId());
            vo.setDescription(cafe.getDescription());
            vo.setName(cafe.getName());
            vo.setCategory(cafe.getCategory());
            vo.setImage(cafe.getImage());
            vo.setPrice(cafe.getPrice());
            return vo;
        }).collect(Collectors.toList());
        return Result.success(coffeQueryVos);
    }

}
